package datamining.algorithms.prefixspan;

import java.util.Collection;
import java.util.TreeSet;

import datamining.miner.chain.Extender;
import datamining.miner.chain.Extension;
import datamining.miner.chain.MiningStep;
import datamining.miner.chain.SearchLatticeNode;
import de.parsemis.algorithms.gSpan.GSpanExtension;

/**
 * <p>
 * 创建于 2012-12-2 下午10:06:57
 * 
 * @author xujinghu (jinghuxu@nudt.edu.cn) <br>
 *         Copyright 2012 xujinghu <br>
 *         Lisence: Apache
 */
public class PrefixSpanExtender<ItemType> extends MiningStep<ItemType> implements
		Extender<ItemType> {

	private TreeSet<SearchLatticeNode<ItemType>> children;

	private MiningStep<ItemType> first;
	private final Collection<Extension<ItemType>> dummy;

	private PThreadEnvironment<ItemType> tenv;
	
	public PrefixSpanExtender(final PThreadEnvironment<ItemType> tenv) {
		super(null);
		first = this;
		this.tenv = tenv;
		dummy = new TreeSet<Extension<ItemType>>();
	}

	public void call(SearchLatticeNode<ItemType> node, Collection<Extension<ItemType>> extensions) {
		// called at the end of the chain, so all extensions are generated and
		// the children can be built
		for (final Extension<ItemType> ext : extensions) {
			children.add(node.extend(ext));
			((PrefixSpanExtension<ItemType>) ext).release(tenv);
		}
	}
	
	@Override
	public Collection<SearchLatticeNode<ItemType>> getChildren(
			Collection<SearchLatticeNode<ItemType>> nodes)
			throws UnsupportedOperationException {
		throw new UnsupportedOperationException(
				"not available for prefixSpan");
	}

	@Override
	public Collection<SearchLatticeNode<ItemType>> getChildren(SearchLatticeNode<ItemType> node)
			throws UnsupportedOperationException {
		assert (node instanceof PrefixCode);

		children = new TreeSet<SearchLatticeNode<ItemType>>();

		dummy.clear();
		// start the run throu the chain for the given node
		first.call(node, dummy);
		return children;
	}

	public void setFirst(final MiningStep<ItemType> first) {
		this.first = first;
	}
}
